# 该镜像需要依赖的基础镜像
FROM centos:7.9.2009  

# 指定维护者的名字
LABEL com.huaweicloud.saaslanding.image.authors="saaslanding-team"

ADD ./docker/* /usr/local/src/
COPY ./docker/mime.types /etc/nginx/

# 外网不需要改repo源
COPY ./docker/yum.repos.d /etc/yum.repos.d


RUN yum install -y gcc g++ gcc-c++ make perl net-tools vim bzip2
WORKDIR /usr/local/src/pcre-8.45
RUN ./configure && make  && make install
WORKDIR /usr/local/src/zlib-1.2.12
RUN ./configure && make && make install
WORKDIR /usr/local/src/openssl-1.1.1q
RUN ./config --prefix=/usr/local/openssl  \
    && make && make install \
    && ln -s /usr/local/openssl/bin/openssl /usr/local/bin/openssl \
    && echo "/usr/local/openssl/lib" >> /etc/ld.so.conf.d/libc.conf \
    && ldconfig
WORKDIR /usr/local/src/luajit2-2.1-20220411
RUN make PREFIX=/usr/local/luajit  \
    && make install PREFIX=/usr/local/luajit  \
    && ln -s /usr/local/luajit/bin/luajit /usr/local/bin/luajit \
    && echo "/usr/local/luajit/lib" >> /etc/ld.so.conf.d/libc.conf \
    && ldconfig \
    && export LUAJIT_LIB=/usr/local/luajit/lib \
    && export LUAJIT_INC=/usr/local/luajit/include/luajit-2.1 \
    && mkdir -vp /usr/local/nginx/lua/ \
    && cp -a  /usr/local/src/lua-resty-redis-0.29/lib /usr/local/nginx/lua/
WORKDIR /usr/local/src/lua-cjson-2.1.0.9
RUN export LUAJIT_LIB=/usr/local/luajit/lib \
    && export LUAJIT_INC=/usr/local/luajit/include/luajit-2.1 \
    && make  \
    && make install \
    && useradd -M -s /sbin/nologin nginx \
    && mkdir -vp /usr/local/nginx/{module,modules,lua,conf} /var/cache/nginx/{client_temp,proxy_temp,fastcgi_temp,uwsgi_temp,scgi_temp} \
    && cp /usr/local/src/lua-cjson-2.1.0.9/cjson.so /usr/local/luajit/lib/lua/5.1/cjson.so
COPY ./docker/redis_cluster /usr/local/nginx/lua/lib/
RUN cd /usr/local/nginx/lua/lib \
    && chmod 555 * \
    && cd resty \
    && chmod 555 *
WORKDIR /usr/local/src/nginx-1.22.0
RUN export LUAJIT_LIB=/usr/local/luajit/lib \
    && export LUAJIT_INC=/usr/local/luajit/include/luajit-2.1 \
    && ./configure \
        --prefix=/usr/local/nginx \
        --user=nginx --group=nginx \
        --with-pcre=../pcre-8.45 \
        --with-zlib=../zlib-1.2.12 \
        --with-openssl=../openssl-1.1.1q \
        --sbin-path=/usr/sbin/nginx \
        --conf-path=/usr/local/nginx/nginx.conf \
        --pid-path=/usr/local/nginx/nginx.pid \
        --error-log-path=/var/log/nginx/error.log \
        --http-log-path=/var/log/nginx/access.log \
        --lock-path=/var/run/nginx.lock \
        --modules-path=/usr/local/nginx/modules \
        --http-client-body-temp-path=/var/cache/nginx/client_temp \
        --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
        --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
        --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
        --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
        --with-threads \
        --with-http_sub_module --with-http_v2_module \
        --with-http_auth_request_module --with-http_realip_module --with-http_secure_link_module \
        --with-http_gunzip_module --with-http_gzip_static_module --with-http_ssl_module \
        --with-http_slice_module --with-http_stub_status_module \
        --with-http_dav_module --with-http_flv_module --with-http_mp4_module \
        --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module  \
        --with-mail --with-mail_ssl_module \
        --with-http_addition_module --with-http_random_index_module \
        --with-compat --with-file-aio \
        --with-cc-opt='-Os -fomit-frame-pointer -g'  \
        --with-ld-opt='-Wl,-rpath,/usr/local/luajit/lib,--as-needed,-O1,--sort-common' \
        --add-module=/usr/local/src/ngx_devel_kit-0.3.1 \
        --add-module=/usr/local/src/lua-nginx-module-0.10.21 \
        --add-dynamic-module=/usr/local/src/echo-nginx-module-0.62 \
    && make  \
    && make install

# Spring Boot 运行时需要 tmp 目录
VOLUME /tmp

COPY dist.tar /tmp/
RUN echo 'Asia/Shanghai'>/etc/timezone \
    && cp /usr/local/src/nginx.conf /usr/local/nginx/ \
    && cp /usr/local/src/set_proxy_pass.lua /usr/local/nginx/conf/ \
    && chmod 777 /usr/local/nginx/conf/set_proxy_pass.lua \
    &&  rm -fr /usr/local/nginx/html/*  \
    && tar -xvf /tmp/dist.tar -C /usr/local/nginx/html/ \
    && cp -R /usr/local/nginx/html/dist/* /usr/local/nginx/html/  \
    && rm -fr /usr/local/nginx/html/dist \
    && rm -fr /tmp/dist.tar  \
    && chown -R nginx:nginx /usr/local/nginx/html

CMD ["nginx","-g","daemon off;"]

ENV app=saaslanding-console-ui \
    profile="product" \
    LANG="en_US.UTF-8"
EXPOSE 80 443